package com.app.twth.ui.b2btrailer;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2bTrailerBinding;
import com.app.twth.ui.b2btrailer.oblpn.B2BTrailerOBLPNActivity;
import com.app.twth.ui.b2btrailer.submit.B2bTrailerQr;
import com.app.twth.utils.DividerItemDecoration;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * @describe :
 */
public class B2BTrailerActivity extends BaseActivity<ActivityB2bTrailerBinding, B2BTrailerViewModel> implements OnRefreshListener {
    private String TAG = getClass().getSimpleName();
    private int index = 0;
    private B2BTrailerAdapter adpter;

    @Override
    protected void initListener() {
        viewModel.b2bTrailerQrLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("B2bTrailerQr", data);
            AppRouter.showActivityForResult(mContext, B2BTrailerOBLPNActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.b2BTrailersLiveData.observe(this, data -> {
            //关闭下拉
            binding.refreshLayout.finishRefresh();
            adpter.setData(data, index);
        });

        viewModel.errorResultCode.observe(this, code -> {
            //关闭下拉
            binding.refreshLayout.finishRefresh();
            showHint(code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "B2BTrailer#";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("");
        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);

        adpter = new B2BTrailerAdapter(this, new ArrayList<>(), position -> {
            b2BTrailer = viewModel.b2BTrailersLiveData.getValue().get(position);
            for (B2BTrailer trailer : viewModel.b2BTrailersLiveData.getValue()) {
                trailer.setClick(false);
            }
            b2BTrailer.setClick(true);
            adpter.notifyDataSetChanged();
            if (index == 0) {
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    return;
                }
            }

            setNext();
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        //添加装饰类
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerView.setAdapter(adpter);

        binding.refreshLayout.setOnRefreshListener(this);
        binding.refreshLayout.autoRefresh();//触发自动刷新
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                clearData();
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                setNext();
                break;
            case R.id.rb_b2b_package:
                if (index == 0) {
                    return;
                }
                index = 0;
                clearData();
                setCheck2Show();
                break;
            case R.id.rb_b2b_ftl:
                if (index == 1) {
                    return;
                }
                index = 1;
                clearData();
                setCheck2Show();
                break;
            case R.id.rb_b2b_ltl:
                if (index == 2) {
                    return;
                }
                index = 2;
                clearData();
                setCheck2Show();
                break;
        }
    }

    private void clearData() {
        binding.tvCode.setText("");
        adpter.setData(new ArrayList<>(), index);
    }

    private void setCheck2Show() {
        binding.rbB2bPackage.setChecked(index == 0 ? true : false);
        binding.rbB2bFtl.setChecked(index == 1 ? true : false);
        binding.rbB2bLtl.setChecked(index == 2 ? true : false);
        switch (index) {
            case 0:
                binding.layoutCode.setVisibility(View.VISIBLE);
                break;
            case 1:
            case 2:
                binding.layoutCode.setVisibility(View.GONE);
                break;
        }
        binding.refreshLayout.autoRefresh();//触发自动刷新
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                EditTextUtil.hideInput(mContext);
            }
            barcodeStr = binding.tvCode.getText().toString();
            return false;
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constant.ForResult2:
                if (resultCode == RESULT_OK) {
                    clearData();
                    binding.refreshLayout.autoRefresh();//触发自动刷新
                }
                break;
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
    }

    B2BTrailer b2BTrailer;

    private void setNext() {
        B2bTrailerQr b2bTrailerQr = new B2bTrailerQr();
        switch (index) {
            case 0:
                barcodeStr = binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    ToastUtil.showToast("Please enter the trailer");
                    return;
                }
                b2bTrailerQr.setTrailerNo(barcodeStr);

                if ((viewModel.b2BTrailersLiveData.getValue() == null) || (viewModel.b2BTrailersLiveData.getValue().size() == 0)) {
                    ToastUtil.showToast("no order");
                    return;
                }
                if (b2BTrailer == null) {
                    ToastUtil.showToast("Please select the order");
                    return;
                }
                break;
            case 1:
            case 2:
                if ((viewModel.b2BTrailersLiveData.getValue() == null) || (viewModel.b2BTrailersLiveData.getValue().size() == 0)) {
                    ToastUtil.showToast("no order");
                    return;
                }
                if (b2BTrailer == null) {
                    ToastUtil.showToast("Please enter the trailer");
                    return;
                }
                break;
        }

        b2bTrailerQr.setType(index + 1);

        if (b2BTrailer != null) {
            b2bTrailerQr.setOrderNo(b2BTrailer.getOrderNo());
            b2bTrailerQr.setRefId(b2BTrailer.getRefId());
        }
        if (index == 0) {
            viewModel.b2bTrailerEntryTrailer(b2bTrailerQr);
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putSerializable("B2bTrailerQr", b2bTrailerQr);
        AppRouter.showActivityForResult(mContext, B2BTrailerOBLPNActivity.class, bundle, Constant.ForResult2);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        b2BTrailer = null;
        viewModel.b2bTrailerPendingJobs(index);
    }

}
